/******************************************************************************
	This file is a part of the Snapshot Bioinformatics Project

	Copyright (C) 2012-2013 Jarom Schow

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#ifndef SNAPSHOT_GENE_COLLECTION_H
#define SNAPSHOT_GENE_COLLECTION_H

#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QHash>
#include <QtCore/QObject>

#include "SeqRange.h"
#include "SqlServer.h"

class QTextStream;

class SnapshotSequence {
public:
	SnapshotSequence();
	SnapshotSequence( int bioentryId, const QString &accession, const Bio::SeqRange &location );
	~SnapshotSequence();

	void	setBioentryId( int id );
	int		getBioentryId() const;

	void	setAccession( const QString &accession );
	QString	getAccession() const;

	Bio::SeqRange	getLocation() const;
	void			setLocation( const Bio::SeqRange &location );

private:
	int				m_bioentryId;
	QString			m_accession;
	Bio::SeqRange	m_location;
};

class SnapshotSequences {
public:
	SnapshotSequences();
	SnapshotSequences( const QString &taxonName );
	virtual ~SnapshotSequences();

	void	setTaxonName( const QString &name );
	QString	getTaxonName() const;

	int					getNumSequences() const;
	SnapshotSequence*	getSequence( int index ) const;

	void	addSequence( int bioentryId, const QString &accession, const Bio::SeqRange &location );

	QListIterator<SnapshotSequence*>	getSequenceIterator() const;

	SnapshotSequence*	chooseSequence( Bio::SqlServer &server ) const;

private:

	QString	m_taxonName;

	QList<SnapshotSequence*>	m_sequences;
};

class SnapshotGene {
	friend class SnapshotGeneCollection;
private:
	SnapshotGene( SnapshotGeneCollection *coll, const QString &name );

public:
	virtual ~SnapshotGene();

	void	setName( const QString &name );
	QString	getName() const;

	void	setSelected( bool onOff );
	bool	isSelected() const;

	int		getNumSequences() const;

	SnapshotSequences*	getSequences( int taxonId );

	void	addSequence( const QString &taxonName, int taxonId, int bioentryId, const QString &accession, const Bio::SeqRange &location );

	int		getNumTaxa() const;

	bool	saveAs( const QString &filename, Bio::SqlServer &server, bool saveName, bool saveGI, bool saveGB ) const;

	QHashIterator<int,SnapshotSequences*>	getSequencesIterator() const;

private:

	QString		getGI( int bioentryId, Bio::SqlServer &server ) const;
	QByteArray	getSequence( int bioentryId, Bio::SqlServer &server ) const;

	bool	m_selected;
	QString	m_name;

	SnapshotGeneCollection			*m_coll;
	QHash<int,SnapshotSequences*>	m_sequences;
};

class SnapshotGeneCollection : public QObject {
	Q_OBJECT
	friend class SnapshotGene;
public:
	SnapshotGeneCollection();
	~SnapshotGeneCollection();

	int				getNumGenes() const;
	int				getNumSelectedGenes() const;
	int				getNumSequences() const;
	SnapshotGene*	getGene( int which ) const;
	SnapshotGene*	getGene( const QString &name );

	QListIterator<SnapshotGene*>	getGeneIterator() const;

	void	clear();

signals:

	void	selectionChanged();
	void	dataChanged();

private:

	void	selectionChanged( SnapshotGene *gene );

	QList<SnapshotGene*>			m_genes;
	QHash<QString,SnapshotGene*>	m_lookup;
};

#endif // SNAPSHOT_GENE_COLLECTION_H